# Makefile for Zig memory management module
# SPDX-License-Identifier: MPL-2.0

ZIG = zig
TARGET = x86_64-freestanding
OPTIMIZE = ReleaseFast

# 源文件
ZIG_SOURCES = src/lib.zig src/frame.zig src/heap.zig src/page_table.zig src/vm_space.zig src/dma.zig
C_SOURCES = src/c_bindings.c

# 输出目录
BUILD_DIR = zig-out
LIB_DIR = $(BUILD_DIR)/lib
BIN_DIR = $(BUILD_DIR)/bin

# 目标文件
STATIC_LIB = $(LIB_DIR)/libasterinas_mm_zig.a
EXAMPLE_BIN = $(BIN_DIR)/basic_usage

.PHONY: all clean test example install help

all: $(STATIC_LIB)

# 构建静态库
$(STATIC_LIB): $(ZIG_SOURCES) $(C_SOURCES) build.zig
	@echo "Building Zig memory management library..."
	$(ZIG) build -Dtarget=$(TARGET) -Doptimize=$(OPTIMIZE)

# 构建示例
example: $(EXAMPLE_BIN)

$(EXAMPLE_BIN): examples/basic_usage.zig $(STATIC_LIB)
	@echo "Building example..."
	$(ZIG) build-exe examples/basic_usage.zig -target $(TARGET) -O $(OPTIMIZE) -femit-bin=$(EXAMPLE_BIN)

# 运行测试
test:
	@echo "Running tests..."
	$(ZIG) build test

# 运行示例（需要QEMU或类似环境）
run-example: $(EXAMPLE_BIN)
	@echo "Running example (requires proper environment)..."
	@echo "Note: This requires a bare-metal environment or emulator"
	# qemu-system-x86_64 -kernel $(EXAMPLE_BIN) -nographic

# 清理构建文件
clean:
	@echo "Cleaning build files..."
	rm -rf $(BUILD_DIR)
	rm -rf zig-cache

# 安装到系统（可选）
install: $(STATIC_LIB)
	@echo "Installing library..."
	@mkdir -p /usr/local/lib/asterinas
	@mkdir -p /usr/local/include/asterinas
	cp $(STATIC_LIB) /usr/local/lib/asterinas/
	cp src/*.zig /usr/local/include/asterinas/

# 代码格式化
format:
	@echo "Formatting Zig code..."
	$(ZIG) fmt src/
	$(ZIG) fmt examples/

# 代码检查
check:
	@echo "Checking Zig code..."
	$(ZIG) build-lib src/lib.zig -target $(TARGET) --check

# 生成文档
docs:
	@echo "Generating documentation..."
	$(ZIG) build-lib src/lib.zig -target $(TARGET) -femit-docs=$(BUILD_DIR)/docs

# 性能测试
benchmark: $(STATIC_LIB)
	@echo "Running benchmarks..."
	$(ZIG) build-exe examples/benchmark.zig -target $(TARGET) -O ReleaseFast
	# ./zig-out/bin/benchmark

# 内存检查（调试模式）
memcheck:
	@echo "Building with memory checking..."
	$(ZIG) build -Dtarget=$(TARGET) -Doptimize=Debug -Dmemory-check=true

# 显示帮助信息
help:
	@echo "Asterinas Zig Memory Management Makefile"
	@echo ""
	@echo "Available targets:"
	@echo "  all        - Build the static library (default)"
	@echo "  example    - Build the basic usage example"
	@echo "  test       - Run all tests"
	@echo "  clean      - Clean build files"
	@echo "  format     - Format Zig source code"
	@echo "  check      - Check code without building"
	@echo "  docs       - Generate documentation"
	@echo "  install    - Install library to system"
	@echo "  memcheck   - Build with memory checking"
	@echo "  help       - Show this help message"
	@echo ""
	@echo "Variables:"
	@echo "  TARGET     - Target architecture (default: $(TARGET))"
	@echo "  OPTIMIZE   - Optimization level (default: $(OPTIMIZE))"
	@echo ""
	@echo "Examples:"
	@echo "  make TARGET=aarch64-freestanding"
	@echo "  make OPTIMIZE=Debug"

# 依赖关系
$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

$(LIB_DIR): $(BUILD_DIR)
	mkdir -p $(LIB_DIR)

$(BIN_DIR): $(BUILD_DIR)
	mkdir -p $(BIN_DIR)